package atlantik.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import modelz.IdMap;
import modelz.UpdateableCollectionListener;

import atlantik.game.Atlantik;
import atlantik.game.GameChoice;

public class GameChooser extends JPanel implements ActionListener {

	private final Atlantik client;

	private final DefaultComboBoxModel model = new DefaultComboBoxModel();

	private final JComboBox games = new JComboBox();
	private final JButton startGame = new JButton();

	private UpdateableCollectionListener<GameChoice> collectionListener = new UpdateableCollectionListener<GameChoice>() {

		public void cleared() {
			model.removeAllElements();
		}

		public void added(GameChoice item) {
			model.addElement(item);
		}

		public void removed(GameChoice item) {
			model.removeElement(item);
		}
	};

	public GameChooser(Atlantik client) {
		this.client = client;
		games.setModel(model);
		startGame.addActionListener(this);
                startGame.setText("Create Game");   //Steven Grier July 1, 2008: Added Text to Start game button
		
		setLayout(new BorderLayout());
		add(games);
		add(startGame, BorderLayout.NORTH); //Steven Grier July 1, 2008: Modified start game button location
		
		IdMap<String, GameChoice> choices = client.getState().gameChoices();
		choices.addListener(collectionListener);
	}

	public void actionPerformed(ActionEvent e) {
		// we only got the startGame button
		GameChoice item = (GameChoice) games.getSelectedItem();
		if( item != null )
			client.newGame(item.getId());
	}

}
